package com.yitianyigexiangfa.sanguoshagaoshou.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yitianyigexiangfa.sanguoshagaoshou.domain.User;
import com.yitianyigexiangfa.sanguoshagaoshou.domain.gson.UserGson;
import com.yitianyigexiangfa.sanguoshagaoshou.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * Created by liubei on 2017/5/3.
 */
@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @RequestMapping(value="/user-list", method=RequestMethod.POST)
    public String listUser(){
        System.out.println("hello world");
        User[] users = userMapper.listUser();
        String json = "";
        Gson gson = new GsonBuilder().create();
        UserGson bean = new UserGson();
        bean.setTotal(users.length);
        bean.setRows(Arrays.asList(users));
        json = gson.toJson(bean);
        System.out.println(json);
        return json;
    }
}
